package com.daxt.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.common.util.DateUtil;
import com.daxt.common.util.HttpWsUtil;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.mapper.sys.PostDeptMapper;
import com.daxt.mapper.sys.PostMapper;
import com.daxt.mapper.sys.UserPostMapper;
import com.daxt.model.dto.FourAPostDto;
import com.daxt.model.dto.FourAUserDto;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.model.service.base.vo.Post;
import com.daxt.model.service.base.vo.PostDept;
import com.daxt.model.service.base.vo.UserPost;
import com.daxt.service.PostService;
import com.daxt.utils.BaseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "岗位")
@RestController
public class PostController {


    @Autowired
    private PostMapper postMapper;
    @Autowired
    private PostDeptMapper postDeptMapper;
    @Autowired
    private PostService postService;
    @Autowired
    private UserPostMapper userPostMapper;
    @Autowired
    private ArchivesFondsMapper archivesFondsMapper;


    @ApiOperation(value = "查询", notes = "")
    @GetMapping("/post")
    public Result<List<Post>> findAll() {
        QueryWrapper<Post> queryWrapper = new QueryWrapper<Post>();

        queryWrapper.orderByAsc("sort");
        List<Post> list = postMapper.selectList(queryWrapper);
        return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
    }


    @ApiOperation(value = "添加", notes = "")
    @PostMapping("/post")
    public Result<String> save(@RequestBody Post entity) {
        if(!StringUtils.isEmpty(entity.getCode())){
            QueryWrapper<Post> queryWrapper = new QueryWrapper<Post>();
            queryWrapper.eq("code",entity.getCode());
            int i = postMapper.selectCount(queryWrapper);
            if(i > 0 )return ResultUtil.error("编号不能重复");

        }
        entity.setCreateTime(LocalDateTime.now().toString());
        int rows = postMapper.insert(entity);
        return ResultUtil.result(rows);
    }

    @ApiOperation(value = "修改", notes = "")
    @PutMapping("/post")
    public Result<String> edit(@RequestBody Post entity) {
        if(!StringUtils.isEmpty(entity.getCode())){
            QueryWrapper<Post> queryWrapper = new QueryWrapper<Post>();
            queryWrapper.eq("code",entity.getCode());
            queryWrapper.notIn("id",entity.getId());
            int i = postMapper.selectCount(queryWrapper);
            if(i > 0 )return ResultUtil.error("编号不能重复");

        }

        int rows = postMapper.updateById(entity);
        return ResultUtil.result(rows);
    }

    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping("/post/{id}")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> del(@PathVariable("id") String id) {
        return ResultUtil.result(postMapper.deleteById(id));

    }


//    @ApiOperation(value = "同步外部岗位信息", notes = "")
//    @GetMapping("/post/sync")
//    @Async("cloudExecutor")
//    @Transactional(rollbackFor = Exception.class)
//    public void syncPostInfo() {
//
//        List<FourAPostDto> fourADtoList = new ArrayList<>();
//            for (int i = 1; i < 100; i++) {
//                List<FourAPostDto> tempList = HttpWsUtil.syscPostData(i + "", "1000");
//                if (BaseUtil.listIsNotNullElement(tempList)) {
//                    fourADtoList.addAll(tempList);
//                }else{
//                    break;
//                }
//                System.out.println("====同步外部岗位信息======执行批次完：" + i);
//            }
//
//        fourADtoList.forEach((FourAPostDto dto) -> {
//            QueryWrapper<Post> qw = new QueryWrapper<Post>();
//            qw.eq("extId", dto.getId());
//            Post bean = postMapper.selectOne(qw);
//            if (null == bean) {
//                bean = new Post();
//                bean.setExtId(dto.getId());
//                bean.setId(Long.parseLong(dto.getId()));
//            }
//            bean.setCode(dto.getCode());
//            bean.setName(dto.getName());
//            bean.setCreateTime(DateUtil.getCurrentTime());
//            bean.setSort(0);
//            bean.setExtId(dto.getId());
//            postService.saveOrUpdate(bean);
//
//
//            if(!StringUtils.isEmpty(dto.getSysDeptId())){
//                PostDept pd = postDeptMapper.selectById(bean.getId());
//                if(null == pd){
//                    pd = new PostDept();
//                    pd.setId(bean.getId());
//                    pd.setDeptId(Long.parseLong(dto.getSysDeptId()));
//                    postDeptMapper.insert(pd);
//                }else{
//                    pd.setDeptId(Long.parseLong(dto.getSysDeptId()));
//                    postDeptMapper.updateById(pd);
//
//                }
//            }
//
//        });
//
//
//
//    }


}
